//
//  LTTabBarPlayButtonView.m
//  聆听FM
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 Dark. All rights reserved.
//



#import "LTTabBarPlayButtonView.h"

#define AngleToRadian(x) (x/180.0*M_PI)

@interface LTTabBarPlayButtonView ()



@end

@implementation LTTabBarPlayButtonView

- (instancetype)init {
    if (self = [super init]) {
        // 布局
        UIImageView *backgoundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_np_normal"]];//130*130
        [self addSubview:backgoundImageView];
        backgoundImageView.frame = CGRectMake(0, 0, 65, 65);
        UIImageView *shadowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_np_playshadow"]];//100 * 100
        [backgoundImageView addSubview:shadowImageView];
        shadowImageView.frame = CGRectMake(0, 0, 65, 65);
        
        self.circleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_np_loop"]];
        [backgoundImageView addSubview:_circleImageView];
        
        self.circleImageView.frame = CGRectMake(5, 5, 55, 55);
        
        //设置用户交互:
        backgoundImageView.userInteractionEnabled = YES;
        self.circleImageView.userInteractionEnabled = YES;
        
        //按钮被点击前:
        [self.playButton setImage:[UIImage imageNamed:@"tabbar_np_play"] forState:UIControlStateNormal];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeImage:) name:@"changeImage" object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginPlay:) name:@"play" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pause:) name:@"pause" object:nil];
    }
    return self;
}

- (void)beginPlay:(NSNotification *)notification
{
    
    [self.playButton setImage:[UIImage imageNamed:@"toolbar_pause_h"] forState:UIControlStateNormal];
    self.timer.paused = NO;
}

- (void)pause:(NSNotification *)notification
{
    
    [self.playButton setImage:[UIImage imageNamed:@"tabbar_np_play"] forState:UIControlStateNormal];
    self.timer.paused = YES;
}

- (void)changeImage:(NSNotification *)notification
{
    NSString *urlString = (NSString *)notification.object;
    [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"placeHolder"]];
    
}


/**  背景图rotation旋转 */
- (void)rotation {
    self.circleImageView.layer.transform = CATransform3DRotate(self.circleImageView.layer.transform, AngleToRadian(72/60.0), 0, 0, 1);
}

#pragma mark - 播放按钮和专辑图片CADisplayLink定时器懒加载
- (UIButton *)playButton {
    if (!_playButton) {
        self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        // 去掉长按高亮
        [_playButton setHighlighted:NO];
        
        // 被点击后 "avatar_bg" 透明
        [_playButton setBackgroundImage:[UIImage imageNamed:@"avatar_bg"] forState:UIControlStateSelected];//168*168
       // [_playButton setImage:[UIImage imageNamed:@"toolbar_pause_h"] forState:UIControlStateSelected];//110*110
        [self addSubview:_playButton];
        _playButton.frame = CGRectMake(5, 5, 55, 55);
        
        // 按钮点击后做的方法
        [_playButton addTarget:self action:@selector(didClickedPlayButton:) forControlEvents:UIControlEventTouchUpInside];
        BOOL isPlay = [[NSUserDefaults standardUserDefaults] boolForKey:@"play"];
        if (isPlay) {
            [self.playButton setImage:[UIImage imageNamed:@"toolbar_pause_h"] forState:UIControlStateNormal];
            self.timer.paused = NO;
        }
        else
        {
            [self.playButton setImage:[UIImage imageNamed:@"tabbar_np_play"] forState:UIControlStateNormal];
            self.timer.paused = YES;
        }
    }
    return _playButton;
}
#pragma mark -- 点击播放按钮:
- (void)didClickedPlayButton:(UIButton *)sender
{
    // 点击和不点击时图交换
    if ([self.delegate respondsToSelector:@selector(playButtonDidClick:)]) {
       // sender.selected = !sender.selected;
       // self.timer.paused = !sender.selected;
        if (sender.selected == YES) {
            self.timer.paused = NO;
        }
        [self.delegate playButtonDidClick:sender.selected];
    }
}

- (UIImageView *)contentImageView {
    if (!_contentImageView) {
        // 声明一个内容视图, 并约束好位置
        _contentImageView = [[UIImageView alloc] init];
        // 绑定到圆视图
        [self.circleImageView addSubview:_contentImageView];
        //        [_contentImageView release];
        self.contentImageView.frame = CGRectMake(5, 5, 45, 45);
        // KVO观察image变化,变化了就启动定时器
        [self.contentImageView addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
        
        // 作圆内容视图背景
        self.contentImageView.layer.cornerRadius = 22.5;
        self.contentImageView.clipsToBounds = YES;
    }
    return _contentImageView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"image"]) {
        // 启动定时器
        self.playButton.selected = YES;
        //self.timer.paused = NO;
    }
}

- (CADisplayLink *)timer
{
    if (!_timer) {
        // 创建定时器, 一秒钟调用rotation方法60次
        self.timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotation)];
        // 手动将定时器加入到事件循环中
        // NSRunLoopCommonModes会使得RunLoop会随着界面切换扔继续使用, 不然如果使用Default的话UI交互没问题, 但滑动TableView就会出现不转问题, 因为RunLoop模式改变会影响定时器调度
        [self.timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    }
    return _timer;
}



@end
